<%@ include file="../inc/init.jsp" %>

<html>
	<head>
		<title>
			Graduate Admission System | University of California - San Diego | 
			Populate Database 
		</title>
		<link type="text/css" rel="stylesheet" href="<%= BASE %>css/main.css">
		<script type="text/javascript" src="<%= BASE %>js/msg.js"></script>
	</head>
	<body>
		<div class="header-top"></div>
		<div class="header">
			<a href="<%= BASE %>admin/index.jsp"><img src="<%= BASE %>img/logo.png"></a>
		</div>
		<div class="content">
		
		<table>
		<tr><td><a href="<%= BASE %>admin/index.jsp">
			<img src="<%= BASE %>img/home.png"></a>&nbsp;&nbsp;
			</td>
			<td><span style="color: #515151;">You are logged in as: <b><%= request.getRemoteUser() %></b></span>&nbsp;</td>
			<td><a href="<%= BASE %>logout.jsp?page=admin">
				<img src="<%= BASE %>img/logout.png"></a>
			</td>
		</tr>
		</table><br>
		
		<% 
			if(request.getParameter("populate") == null)
			{
		%>
			<h2><span style="color: #6d0909">WARNING!</span></h2>
			
			<div class="error" style="padding-left: 10px; width: 350px;">
			Do you really want to populate the Database?<br>
			This could cause a loss of custom data already inserted!
			</div>	
			<br>
			<a href="populate.jsp?populate=yes"><img src="<%= BASE %>img/yes.png" alt="Yes"></a> &nbsp;&nbsp;
			<a href="index.jsp"><img src="<%= BASE %>img/no.png" alt="No"></a> &nbsp;&nbsp;	
					
		<%
			}
			else if(request.getParameter("populate").equals("yes"))
			{
		%>
		
			<div class="code">
		
		<%
			/** Prepare support stuffa and vector paths **/
		
			Support s = new Support();
	
			String path1 = config.getServletContext().getRealPath("inc/countries.txt");
   			String path2 = config.getServletContext().getRealPath("inc/universities.txt");
    		String path3 = config.getServletContext().getRealPath("inc/majors.txt");
    		String path4 = config.getServletContext().getRealPath("inc/specializations.txt");
    
		    Vector countries = s.getCountries(path1);
    		ArrayList<String> countriesAl = new ArrayList<String>(); 
    		ArrayList<String> statesAl = new ArrayList<String>();
	    
    		Vector universities = s.getUniversities(path2);
    		ArrayList<ArrayList<String>> universitiesAl = new ArrayList<ArrayList<String>>();
		    
    		Vector discipline = s.getMajors(path3);
    		ArrayList<String> disciplinesAl = new ArrayList<String>();
		    
    		Vector specializations = s.getSpecializations(path4);
    		ArrayList<String> specializationsAl = new ArrayList<String>();
		    
    		/** Call clean method **/
		    
    		if(Populate.clean())
    		{
    			out.print("Clean process was successful.<br>");
		 
    		/** Get all states **/
		    
    		for(int i = 0; i<universities.size(); i++)
    		{
    			Vector tuple = (Vector)universities.get(i);
    			statesAl.add(tuple.get(0).toString());
		    	
    			if(tuple.get(0).toString().equals("Wyoming"))
    			{
    				break;
    			}
    		}
		    
    		/** Get all countries **/
		    
    		for(int i = 0; i<countries.size(); i++)
    		{
    			countriesAl.add(countries.get(i).toString());
    		}    
		    
    		/** Populate location table **/
		    
    		out.print(Populate.location(countriesAl, statesAl));
    
    		/** Get all specializations **/
		    
    		for(int i = 0; i<specializations.size(); i++)
    		{
    			specializationsAl.add(specializations.get(i).toString());
    		}
		    
    		/** Populate specialization table **/
    
		    out.print(Populate.specialization(specializationsAl));
    	
    		/** Get all majors **/
	    
    		for(int i = 0; i<discipline.size(); i++)
    		{
    			disciplinesAl.add(discipline.get(i).toString());
    		}    
		    
    		/** Populate disciplines table **/
		    
    		out.print(Populate.major(disciplinesAl));
		    
    		/** Get all universities **/
		    
    		for(int i = 0; i<universities.size(); i++)
    		{
        		Vector tuple = (Vector)universities.get(i);
		        
		        ArrayList<String> cal = new ArrayList<String>();
		        
				cal.add(tuple.get(0).toString());
		        
				Vector u = (Vector)tuple.get(1);
				
		        for(int j = 0; j<u.size(); j++)
		        {
        			cal.add(u.get(j).toString());
        		}
        		universitiesAl.add(cal);
    		}
		    
    		/** Populate university table **/
		    
    		out.print(Populate.university(universitiesAl));
    		
    		}
    		else
    		{
    			out.print("Error during clean process! There are already data inserted!");
    		}
    		
			}
		%>
			</div>
		</div>
	</body>
</html>